<template>
  <div style="margin:20px;">
    <el-button type="primary" @click="showEidet = true">添加礼物</el-button>
    <el-table :data="giftList" border style="width: 100%;margin-top:20px">
      <el-table-column prop="name" align="center" label="礼物名称" width="180"></el-table-column>
      <el-table-column prop="photo" label="礼物图片" width="180">
        <template slot-scope="scope">
          <img :src="scope.row.photo" alt style="width:80px;height:80px" />
        </template>
      </el-table-column>
      <el-table-column prop="price" label="礼物价格"></el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            v-if="scope.row.status == null ?false : scope.row.status == 0 ?false :true"
            @click="startGift(scope.row,0)"
            type="danger"
          >停用</el-button>
          <el-button size="mini" v-else @click="startGift(scope.row,1)" type="success">启用</el-button>
          <el-button size="mini" @click="deleteGift(scope.row)">删除</el-button>
          <el-button size="mini" @click="eidetGift(scope.row)" type="primary">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="编辑信息" :visible.sync="showEidet" width="30%" :before-close="handleClose">
      <el-form :model="addObj">
        <el-form-item label="礼物名称：" prop="name">
          <el-input v-model="addObj.name" style="width:60%;"></el-input>
        </el-form-item>
        <el-form-item label="礼物价格：" prop="name">
          <el-input v-model="addObj.price" style="width:60%;"></el-input>
        </el-form-item>
        <el-form-item label="礼物图片：" prop="name">
          <el-upload
            :action="imageUploadUrl"
            :show-file-list="false"
            list-type="picture-card"
            :limit="1"
            name="avatar"
            :before-upload="handleBeforeUpload"
            :on-success="handleBannerSuccess"
            :on-remove="handleRemove"
          >
            <img v-if="addObj.photo" :src="addObj.photo" class="avatar" />
            <i v-else class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showEidet = false">取 消</el-button>
        <el-button type="primary" @click="addGift">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { Live, giftSet,giftDel } from "@/api/member";
export default {
  data() {
    return {
      giftList: [],
      showEidet: false,
      addObj: {
        name: "",
        photo: "",
        price: "",
        status: ""
      },
      imageUploadUrl: process.env.BASE_API + "/common/gift/imageUpload"
    };
  },
  watch: {},
  methods: {
    handleClose(){
      this.showEidet = false
      this.addObj = {};
    },
    handleBannerSuccess(val) {
      this.addObj.photo = val.data.imageUrl;
    },
    handleRemove(val) {
      console.log(val);
    },
    handleBeforeUpload(file) {
      console.log("上传文件之前触发啊、。。。");
      const isImg =
        file.type == "image/jpeg" ||
        file.type == "image/png" ||
        file.type == "image/gif";
      if (!isImg) {
        this.$message.warning("上传图片只能是 JPG 或 PNG 格式");
      }
      const isLt3M = file.size / 1024 / 1024 < 3;
      if (!isLt3M) {
        this.$message.error("上传图片大小不能超过 3MB!");
      }
      return isImg && isLt3M;
    },
    eidetGift(data) {
      this.addObj = {
        name: data.name,
        photo: data.photo,
        price: data.price,
        status: 1,
        giftId:data.giftId
      };
      this.showEidet = true;
    },
    addGift() {
      if (this.addObj.name == "") {
        this.$message.warning("请填写礼物名称");
      }

      giftSet(this.addObj).then(res => {
        this.$message.info(res.data.data);
      });
      this.getData();
      this.addObj = {};
      console.log(this.addObj);
      
      this.showEidet = false;
    },
    async startGift(data, val) {
      var datas = {
        giftId: data.giftId,
        name: data.name,
        photo: data.photo,
        price: data.price,
        status: val
      };
      await giftSet(datas).then(res => {
        console.log(res);
        this.$message.success(res.data.data);
      });
      this.getData();
    },
    getData() {
      Live().then(res => {
        this.giftList = res.data.data.data;
        console.log(this.giftList);
      });
    },
    async deleteGift(val) {
      await giftDel({giftId:val.giftId}).then(res =>{
        this.$message.success(res.data.data);
      })
      this.getData();
    }
  },
  mounted() {
    this.getData();
  }
};
</script>
<style scoped>
</style>
